package com.shenganyuan.smac.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shenganyuan.smac.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author Damon
 */
public class CommonSelectDialogAdapter extends RecyclerView.Adapter<CommonSelectDialogAdapter.ViewHolder> {

    private Context mContext;
    private List<String> mDataList;
    private HashSet<Integer> exceptedDataIndex = new HashSet<>();
    private String[] mDataArray;
    private LayoutInflater mInflater;

    private int mIndexChecked;
    private OnRecyclerItemClickListener mOnItemClickListener;

    public CommonSelectDialogAdapter(Context context, List<String> data, ArrayList<Integer> exceptedDataIndex) {
        mContext = context;
        mDataList = data;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        addExcepted(exceptedDataIndex);
    }

    public CommonSelectDialogAdapter(Context context, String[] data, ArrayList<Integer> exceptedDataIndex) {
        mContext = context;
        mDataArray = data;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        addExcepted(exceptedDataIndex);
    }

    private void addExcepted(ArrayList<Integer> exceptedDataIndex){
        this.exceptedDataIndex.clear();
        if (exceptedDataIndex != null){
            for (Integer item : exceptedDataIndex){
                if (item != null){
                    this.exceptedDataIndex.add(item);
                }
            }
        }
    }

    public void setOnItemClickListener(OnRecyclerItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public String getItemData(int position) {
        if (mDataList != null && mDataList.size() > 0) {
            if (position >= 0 && position < mDataList.size()) {
                return mDataList.get(position);
            }
        }
        if (mDataArray != null && mDataArray.length > 0) {
            if (position >= 0 && position < mDataArray.length) {
                return mDataArray[position];
            }
        }
        return null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.common_select_dialog_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String str = getItemData(position);
        if (str != null && str.length() > 0) {
            holder.setText(str);
            holder.setBackground(position);
            holder.setClickListener(position);
            holder.setEnable(checkEnable(position));
        }
    }

    private boolean checkEnable(int position){
        if (exceptedDataIndex == null){
            return true;
        }
        return !exceptedDataIndex.contains(position);
    }

    @Override
    public int getItemCount() {
        if (mDataList != null && mDataList.size() > 0) {
            return mDataList.size();
        }
        if (mDataArray != null && mDataArray.length > 0) {
            return mDataArray.length;
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private RelativeLayout root;

        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView != null) {
                name = itemView.findViewById(R.id.common_select_dialog_item_txt);
                root = itemView.findViewById(R.id.root);
            }
        }

        public void setText(String text) {
            if (name != null) {
                name.setText(text);
            }
        }

        public void setEnable(boolean enable){
            if (enable){
                name.setTextColor(mContext.getResources().getColor(R.color.PrimaryContentColor));
            }else{
                name.setTextColor(mContext.getResources().getColor(R.color.PrimaryContentColorLight1));
            }
        }


        public void setBackground(int position) {
            if (position == getItemCount() - 1) {
                root.setBackgroundResource(R.drawable.bg_dialog_foot_corner);
            } else {
                root.setBackgroundResource(R.drawable.bg_dialog_item);
            }
        }

        public void setClickListener(int position) {
            itemView.setOnClickListener(v -> {
                mIndexChecked = position;
                notifyDataSetChanged();
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(itemView, position);
                }
            });
        }
    }
    public interface OnRecyclerItemClickListener {
        void onItemClick(View parent, int position);
    }

}
